//
//  CommonTableViewController.m
//  EUCIOS
//
//  Created by Khang on 3/29/13.
//  Copyright (c) 2013 Khang. All rights reserved.
//

#import "CommonTableViewController.h"

@interface CommonTableViewController ()

@end

@implementation CommonTableViewController

@synthesize emptyView = _emptyView;
@synthesize errorView = _errorView;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        self.hasSearchBar = NO;
        self.searchBar = nil;
        self.items = [NSMutableArray array];
    }
    return self;
}

- (id)init {
    if (self = [self initWithStyle:UITableViewStylePlain]) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if (self.hasSearchBar) {
        UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.f)];
        self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.f)];
        self.searchBar.barStyle = UIBarStyleBlack;
        self.searchBar.delegate = self;
        
        [headerView addSubview:self.searchBar];
        self.tableView.tableHeaderView = headerView;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)dealloc {
    self.hasSearchBar = NO;
    self.searchBar = nil;
    
    if (self.items) {
        [self.items removeAllObjects];
        self.items = nil;
    }
    
    if (self.emptyView) {
        [self.emptyView removeFromSuperview];
    }
    self.emptyView = nil;
    
    if (self.errorView) {
        [self.errorView removeFromSuperview];
    }
    self.errorView = nil;
}

#pragma mark - UISearchBarDelegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    [searchBar setShowsCancelButton:YES animated:YES];
    return YES;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar {
    [searchBar resignFirstResponder];
    [searchBar setShowsCancelButton:NO animated:YES];
}

#pragma mark - Private
- (Class)cellClassForObject:(id)object ofTableView:(UITableView *)tableview {
    if (object && [object isKindOfClass:[CommonTableItem class]]) {
        return [CommonTableCell class];
    }
    return [CommonTableCell class];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1; //Plain Table
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.items count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Class cellClass = [self cellClassForObject:[self.items objectAtIndex:indexPath.row] ofTableView:tableView];
    NSString* cellIdentifier = NSStringFromClass(cellClass);
    CommonTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[cellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    if ([cell isKindOfClass:[CommonTableCell class]]) {
        [cell setObject:[self.items objectAtIndex:indexPath.row]];
    }
    
    return cell;
}

#pragma mark -
#pragma mark Table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    id object = [self.items objectAtIndex:indexPath.row];
    Class cellClass = [self cellClassForObject:object ofTableView:tableView];
    return [cellClass heightForCellWithObject:object ofTableView:tableView];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark -
#pragma mark TKErrorViewDelegate
- (void)retryButtonPressed {
    //Do something for retry loading for example
}

- (NSString *)titleForEmptyView {
    return nil;
}
- (NSString *)titleForErrorView {
    return nil;
}
- (NSString *)titleForRetryButton {
    return nil;
}

- (NSString *)subTitleForEmptyView {
    return nil;
}
- (NSString *)subTitleForErrorView {
    return nil;
}

- (UIImage *)imageForEmptyView {
    return nil;
}
- (UIImage *)imageForErrorView {
    return nil;
}

@end
